﻿using CSharpDay12__Many_to_Many_.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CSharpDay12__Many_to_Many_.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<IndexShipViewModel> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Ships.Select(x => new IndexShipViewModel()
                {
                    Id = x.Id,
                    IsAtSea = x.IsAtSea,
                    ShipName = x.Name,
                    MaxWeight = x.GrossTonnage,
                    Size = x.CanalSize,
                    //adds the total weight via the sum Linq statement + the ships base weight
                    //Tenary statement used here. If the ships cargoes is greater than zero then do the cargoe/weight calculation ELSE set grossweight equal to baseweight.
                    GrossWeight = (x.ShipsCargoes.Count > 0) ? x.ShipsCargoes.Sum(s => s.Containers * s.Cargoe.Weight) + x.BaseWeight : x.BaseWeight
                }).ToList();
            }
            return View(model);
        }

        public ActionResult AddShip()
        {
            ViewBag.Message = "New ship to our fleet.";

            return View(new Ship());
        }

        [HttpPost]
        public ActionResult AddShip(Ship model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    db.Ships.Add(model);
                    db.SaveChanges();
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult AddCargoe(int? Id)
        {
            Ship_Cargoe model = new Ship_Cargoe();
            model.ShipId = Id.Value;

            return View(model);
        }

        [HttpPost]
        public ActionResult AddCargoe(Cargoe model)
        {
            if (ModelState.IsValid)
            {
                using (ApplicationDbContext db = new ApplicationDbContext())
                {
                    db.Cargoes.Add(model);
                    db.SaveChanges();
                }
            }

            return RedirectToAction("Index");
        }

        //public ActionResult ViewCargoe(int? Id)
        //{
        //    List<IndexViewCargoe> model;
        //    using (ApplicationDbContext db = new ApplicationDbContext())
        //    {
        //        model = db.Cargoes.Select(x => new IndexViewCargoe()
        //        {
        //            Id = x.Id,
        //            Name = x.Name,
        //            Weight = x.Weight,
        //            IsPerishable = x.IsPerishable,
        //            CargoeType = x.CargoeType
        //        }).ToList();

        //    }
        //    return View("CargoePeek");
        //}
        public ActionResult ViewCargoe(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            List<Cargoe> model;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Ships_Cargoes.Where(x => x.ShipId == id).Select(x => x.Cargoe).ToList();
            }
            if (model == null)
            {
                return RedirectToAction("Index");
            }
            return View(model);
        }

        // Cargo View
        public ActionResult CargoePeek()
        {
            List<IndexViewCargoe> model;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                model = db.Cargoes.Select(x => new IndexViewCargoe()
                {
                    Id = x.Id,

                    Name = x.Name,
                    Weight = x.Weight,
                    IsPerishable = x.IsPerishable,
                    CargoeType = x.CargoeType
                }).ToList();
            }
            return View(model);
        }
    }
}